<?php
class DataTableRendererDiary extends DataTableRenderer
{

	public function getHTML()
	{
		$html = "<SCRIPT language='javascript'>\n";
		$html .= "function ".$this->dataTable->getName()."Refresh()\n";
		$html .= "{\n";
//		$html .= "document.getElementById(\"Action\").value = \"".$this->action."\";\n";
		$html .= "document.getElementById(\"PageForm\").submit();\n";
		$html .= "}\n";
		$html .= "</SCRIPT>\n";

		$hidden = new Hidden();
		$hidden->setName("ShowSearchCriteria");
		$hidden->setValue('table');
		$html .= $hidden->gethtml();

		$RecordID = new Hidden();
		$RecordID->setName("RecordID");
		$html .= $RecordID->getHTML();

		$DBTableName = new Hidden();
		$DBTableName->setName("DBTableName");
		$html .= $DBTableName->getHTML();

		$DBTableName = new Hidden();
		$DBTableName->setName("NewRecordDate");
		$html .= $DBTableName->getHTML();
		
		$html .= "<script language=\"javascript\">\n";
		$html .= "function toggleFilterPanel()\n";
		$html .= "{\n";
//		$html .= "alert (\"Hello\");\n";
		$html .= "criteria = document.getElementById(\"ShowSearchCriteria\");\n";
		$html .= "table = document.getElementById(\"filterPanel\");\n";
		$html .= "text = document.getElementById(\"filterText\");\n";
//		$html .= "alert (text.innerHTML);\n";
		$html .= "if(text.innerHTML == 'Click to show search criteria')\n";
		$html .= "{\n";
		$html .= "table.style.display = 'table';\n";
		$html .= "text.innerHTML = 'Click to hide search criteria'\n";
		$html .= "criteria.value='table';\n";
		$html .= "}\n";
		$html .= "else\n";
		$html .= "{\n";
		$html .= "table.style.display = 'none';\n";
		$html .= "text.innerHTML = 'Click to show search criteria'\n";
		$html .= "criteria.value='none';\n";
		$html .= "}\n";
		$html .= "}\n";
		$html .= "</script>\n";
		
		if($this->dataTable->getAreEditable())
		{
			$newButton1 = new Button();
			$newButton1->setName("newButton");
			$newButton1->setValue("New Record");
			$newButton1->setOnClick("newRecord('".$this->dataTable->getDBTableName()."');");
			$html .= "<tr><td>".$newButton1->getHTML()."</tr></td>";
		}
		
		$display = 'none';
		if(isset($_POST['ShowSearchCriteria']))
			$display = $_POST['ShowSearchCriteria'];

		$text = ($display=='none')?"Click to show search criteria":"Click to hide search criteria";
		
			
		$html .= "<h3><a id=\"filterText\" href='javascript:toggleFilterPanel()'>".$text."</a></h3>\n";
		$html .= "<p><table style=\"{display:".$display.";}\" id=\"filterPanel\">";

		// Filter Requests

		foreach ($this->dataTable->getFilterRequests() as $aFilterRequest)
		{
			$html .= $aFilterRequest->getHTML();
		}

		if($this->dataTable->getAreEditable())
		{
			$refreshButton1 = new Button();
			$refreshButton1->setName("refreshButton");
			$refreshButton1->setValue("Refresh");
			$refreshButton1->setOnClick($this->dataTable->getName()."Refresh('".$this->dataTable->getDBTableName()."');");
			$html .= "<tr><td>".$refreshButton1->getHTML()."</tr></td>";
		}

		$html .= "</table></p>";


		$html .= "</table></p>";

		$this->dataTable->getDatabase()->ExecuteSQL($this->dataTable->getSQLCommand());

//		echo "Last SQL = ".$this->dataTable->getSQLCommand()."<br>";
		$previousDate = null;
		$cells = array();
		$cell = null;

		$users = new Enumeration($this->dataTable->getDatabase(),'SnapUsers');
		$eventTypes = new Enumeration($this->dataTable->getDatabase(),'EventTypes');

		$locations = new Enumeration($this->dataTable->getDatabase(),'Locations');
		$eventInvolvees = new Enumeration($this->dataTable->getDatabase(),'EventInvolvees');

		$DBTableName = $this->dataTable->getDBTableName();
		while($row=$this->dataTable->getDatabase()->getRow())
		{
			echo mysql_error();
			if(!isset($previousDate))
			{
				$previousDate = new DateTime($row["Start"]);
				$previousDate->modify("-1 day");
			}
			


//INVOLVEES
			$sql = "SELECT id.EventInvolveeNameID, n.ShortName FROM EventInvolvees as id INNER JOIN EventInvolveeNames as n ON id.EventInvolveeNameID = n.EventInvolveeNameID WHERE id.EventID = ".$row['EventID'];
			$database = $this->dataTable->getDatabase()->Recopy();
			$database->ExecuteSQL($sql);
			$theInvolvees = array();
			while($row1 = $database->getRow())
			{
				array_push($theInvolvees, $row1["ShortName"]);
			}


//LOCATIONS
			$sql = "SELECT id.LocationNameID, n.ShortName FROM Locations as id INNER JOIN LocationNames as n ON id.LocationNameID = n.LocationNameID WHERE id.EventID = ".$row['EventID'];
			$database = $this->dataTable->getDatabase()->Recopy();
			$database->ExecuteSQL($sql);
			$theLocations = array();
			while($row1 = $database->getRow())
			{
				array_push($theLocations, $row1["ShortName"]);
			}

			$emptyarray = array();
			$eventID = $row["EventID"];
			
			$event = new Event(
				$eventID,
				$row["CreatedWhen"],
				$row["CreatedBy"],
				$row["DeletedWhen"],
				$row["DeletedBy"],
				$row["Start"],
				$row["End"],
				$row["Title"],
				$row["Description"],
				$row["Comment"],
				$row["Responsible"],
				$theInvolvees,
				$row["EventTypeID"],
				$theLocations,
				$users,
				$locations,
				$eventTypes,
				$eventInvolvees);

			$date = new DateTime($row["Start"]);
//			echo $date->format("Y-m-d")." ".$row["Title"]."<br>";
			if($date->format("Ymd")!=$previousDate->format("Ymd"))
			{
				while($date->format("Ymd")!=$previousDate->format("Ymd"))
				{
					if (isset($cell))array_push($cells,$cell);

					$previousDate->modify("1 day");
					$cell = new DiaryCell($DBTableName, $previousDate->format("Y-m-d"));
				}
			}
			$cell->addEvent($event);
		}
		if (isset($cell))array_push($cells,$cell);

		$i= 0;

		// get day of week and month of first cell
		if(isset($cells[0]))$firstDate = new DateTime($cells[0]->getDate());
		else $firstDate = new DateTime("now");
		$firstDoW = $firstDate->format("w");
//		$firstMonth = $firstDate->format("F Y");

		$html .= "<div id=\"container\">\n";

		$html .= "<p class=\"diarycaption\">".$firstDate->format("F Y")."</p>\n";
		$html .= "<div id=\"heading\">Sunday</div>\n";
		$html .= "<div id=\"heading\">Monday</div>\n";
		$html .= "<div id=\"heading\">Tuesday</div>\n";
		$html .= "<div id=\"heading\">Wednesday</div>\n";
		$html .= "<div id=\"heading\">Thursday</div>\n";
		$html .= "<div id=\"heading\">Friday</div>\n";
		$html .= "<div id=\"heading\">Saturday</div>\n";
		$html .= "<div class=\"floatstop\"></div>\n";

		for ($j=0;$j<$firstDoW;$j++)
		{
			$html .= "<div id=\"cell\" >&nbsp;</div>";
			$i++;
		}

		foreach ($cells as $aCell)
		{
			$curDate = new DateTime($aCell->getDate());
			if($curDate->format("d")=="01")
			{
				while($i++<7)$html .= "<div id=\"cell\" >&nbsp;</div>";
				$html .= "<div class=\"floatstop\"></div>\n";
				$html .= "<p class=\"diarycaption\">".$curDate->format("F Y")."</p>\n";
				$html .= "<div id=\"heading\">Sunday</div>\n";
				$html .= "<div id=\"heading\">Monday</div>\n";
				$html .= "<div id=\"heading\">Tuesday</div>\n";
				$html .= "<div id=\"heading\">Wednesday</div>\n";
				$html .= "<div id=\"heading\">Thursday</div>\n";
				$html .= "<div id=\"heading\">Friday</div>\n";
				$html .= "<div id=\"heading\">Saturday</div>\n";
				$html .= "<div class=\"floatstop\"></div>\n";
				$i = 0;
				for ($j=0;$j<($curDate->format("w"));$j++)
				{
					$html .= "<div id=\"cell\" ></div>";
					$i++;
				}
			}

			$html .= $aCell->getHTML();
			if ($i++>5) 
			{
				$i=0;
				$html .= "<div class=\"floatstop\"></div>\n";
			}
		}
		while($i++<7)$html .= "<div id=\"cell\" ></div>";
		$html .= "<div class=\"floatstop\"></div></div>\n";
//		echo microtime()." end<br>";

		return $html;
 	}

}

?>